let path = '/post/:id/:age';

let pathname = '/post/100/200';

function compilePath(path) {
    let paramNames = [];
    let regexpSource = "^" + path.replace(/:(\w+)/g, (_, key) => {
        paramNames.push(key);
        return "([^\\/]+)";
    })
    regexpSource += '$';
    let matcher = new RegExp(regexpSource);
    return [matcher, paramNames];
}
let [matcher, paramNames] = compilePath(path);
console.log(matcher);  //   /^\/post\/([^\/]+)\/([^\/]+)$/
console.log(paramNames); //    [ 'id', 'age' ]
let match = pathname.match(matcher);
console.log(match);
// [
//     '/post/100/200',
//     '100',
//     '200',
//     index: 0,
//     input: '/post/100/200',
//     groups: undefined
// ]
let values = match.slice(1);
let params = paramNames.reduce((memo, paramName, index) => {
    memo[paramName] = values[index];
    return memo;
}, {});
console.log(params); //  { id: '100', age: '200' }